﻿using System;
using System.Web.Security;
using System.Web.Services;
using QuanLyHopDong.Biz;
using QuanLyHopDong.Biz.Model;
using QuanLyHopDong.Web.Common;

namespace QuanLyHopDong.Web.Employees
{
    public partial class List : System.Web.UI.Page
    {
        #region Properties

        private static EmployeeBiz _employeeBiz;
        private static EmployeeBiz EmployeeManager
        {
            get { return _employeeBiz ?? (_employeeBiz = new EmployeeBiz()); }
        }

        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            if (WebCommon.CurrrentEmployee == null)
            {
                FormsAuthentication.RedirectToLoginPage();
                return;
            }

            if (!WebCommon.CurrrentEmployee.IsAdmin)
            {
                Response.Redirect("~/NoPermission.aspx");
            }
        }

        #region Web methods

        [WebMethod(EnableSession = true)]
        public static object EmployeeList(int jtStartIndex, int jtPageSize, string jtSorting, string name)
        {
            int totalRecords;
            var employees = EmployeeManager.GetEmployees(jtStartIndex, jtPageSize, jtSorting, name, out totalRecords);

            return new { Result = "OK", Records = employees, TotalRecordCount = totalRecords };
        }
        
        [WebMethod(EnableSession = true)]
        public static object DeleteEmployee(int Id)
        {
            if (WebCommon.CurrrentEmployee != null && WebCommon.CurrrentEmployee.IsAdmin && WebCommon.CurrrentEmployee.Id != Id)
            {
                EmployeeManager.DeleteEmployee(Id);
                return new {Resutl = "OK"};
            }
            return new { Result = "ERROR", Message = "Error" };
        }

        #endregion
    }
}